<?php 
/**
 * 问题管理控制器
 * @author 阿柱 <houdunwangmzy@163.com>
 */
class AskController extends CommonController{
    /**
     * 显示问题
     */
    public function index(){
//		判断session
		$this->judgeSession();
		
//		导航条内容处理Start***************
//		导航条头部的问题库
		$this->topCate();
//		导航条右侧的累计提问数
		$this->topCount();
//		导航条处理结束End*************

//		获取cid
		$cid=isset($_GET['cid'])? (int)$_GET['cid'] : 0;
//		处理面包屑导航
//		就是找父级的过程，需要用递归，因为不确定有多少个父级
//		1.将所有的分类查询出来
		$sql="SELECT * FROM hd_category";
		$data=M()->query($sql);
		$fatherCate=$this->_fatherCate($data, $cid);
//		数组反转
		$fatherCate=array_reverse($fatherCate);
		$this->assign('fatherCate',$fatherCate);
		
//		查询问题表***********
		$asid=isset($_GET['asid'])? (int)$_GET['asid'] : 0; 
		
		$sql="SELECT * FROM hd_ask AS a JOIN hd_user as u ON 
		a.uid=u.uid WHERE a.asid={$asid}";

		$askData=M()->query($sql);
		$askData[0]['lv']=$this->expToLv($askData[0]['exp']);
//		p($askData);
		$this->assign('askData',$askData[0]);
		
//		查询问题回复表***********
		$sql="SELECT * FROM hd_ask AS a JOIN hd_answer AS an ON a.asid=an.asid JOIN hd_user as u ON an.uid=u.uid WHERE a.asid={$asid}";
		
		$questionData=M()->query($sql);
//		p($questionData);
		$this->assign('questionData',$questionData);
//		分配用户uid变量
		$sql="SELECT * FROM hd_ask AS a JOIN hd_user AS u ON a.uid=u.uid WHERE a.asid={$asid}";
//		p($sql);
		$askUser=M()->query($sql);
//		P($askUser);
		$this->assign('askUser',$askUser[0]);
		
//		统计总回答数
		$sql="SELECT count(*) AS c FROM hd_ask AS a JOIN hd_answer AS an ON a.asid=an.asid WHERE a.asid={$asid}";
//		p($sql);
		$countQues=M()->query($sql);
		$this->assign('countQues',$countQues[0]);
		
		//待解决的相关问题***********
		$sql="SELECT * FROM hd_ask AS a WHERE cid={$cid} ORDER BY RAND() LIMIT 5";
//		p($sql);
		$about=M()->query($sql);
//		p($about);
		$this->assign('about',$about);
		
		
		//满意答案***********
		$sql="SELECT * FROM hd_ask AS a JOIN hd_answer AS an ON a.asid=an.asid JOIN hd_user as u ON an.uid=u.uid WHERE a.SOLVE=1 and an.accept=1 and a.asid={$asid}";
		$satisfy=M()->query($sql);
//		判断一下是否为空
		if(!empty($satisfy)){
//		调用等级
		$satisfy[0]['lv']=$this->expToLv($satisfy[0]['exp']);
		$satisfy[0]['c']=$satisfy[0]['accept'] / $satisfy[0]['answer'] *100 ;	
		$satisfy[0]['c']=round($satisfy[0]['c'],2). '%';		
		}

		$this->assign('satisfy',$satisfy);
//		p($satisfy);
		
		//右侧数据数据处理Start**********		
		//今日之星
		$this->dayStar();		
		//历史回答最多的人
		$this->historyStar();		
		//历史帮助最多的人
		$this->helpPeople();
		//右侧数据数据处理End**********
		
    	$this->display("show.html");
    }
	
	/**
	 * 采纳
	 */
	public function accept(){
		
		$anid=$_GET['anid'];
		$uid=$_GET['uid'];
		$userid=$_GET['user'];
		$asid=$_GET['asid'];

		//1.把问题表标记为已解决		
		$sql="UPDATE hd_ask SET solve=1 WHERE asid={$asid}";	
		M()->exec($sql);

		//2.把答案表的accept字段改为1就是，采纳了
		$sql="UPDATE hd_answer SET accept=1 WHERE anid={$anid}";
		M()->exec($sql);
		
		//3.给被采纳用户加采纳数，金币，经验
		$sql="SELECT * FROM hd_ask WHERE uid={$userid} AND asid={$asid}";
		$reward=M()->query($sql);
		$point=$reward[0]['reward'];
		$sq="UPDATE hd_user SET accept=accept+1,point=point+{$point},exp=exp+10";
//		p($sql);
		M()->exec($sql);
		$this->error("采纳成功");
	}
	
	/**
	 * 回答
	 */
	 
	public function answer(){
		//1.给hd_answer插入一条数据
		//2.给回答者hd_user表回答数加1
		//3.给问题表hd_ask表回答数加1
		$uid=(int)$_GET['uid'];          //$uid=$_SESSION['uid'];
		$cid=(int)$_GET['cid'];		
		$asid=(int)$_GET['asid'];
		$time=time();  //获取时间戳		
//		问题表answer+1		
		$sql="UPDATE hd_ask SET answer=answer+1 WHERE asid={$asid}";
		M()->exec($sql);
		
//		用户 回答数加1，经验加2,金币+1	
		$sql="UPDATE hd_user SET answer=answer+1,exp=exp+2,point=point+1 WHERE uid={$uid}";
		M()->exec($sql);
		
//		给回答表插入数据
		$sql="INSERT INTO hd_answer SET content='{$_POST['content']}',time=($time),uid={$uid},asid={$asid}";
		M()->exec($sql);
		
		$this->error('回复成功');
		
	}
	
	/**
	 * 提问操作
	 */
	
	public function ask(){
		
		$this->topCate();
//		导航条右侧的累计提问数
		$this->topCount();
//		如果session存在的话
		if(isset($_SESSION['uid']) && isset($_SESSION['uname'])){
			$uid=$_SESSION['uid'];
			$sql="SELECT * FROM hd_user WHERE uid={$uid}";
			$data=M()->query($sql);
			$this->assign('user',$data[0]);		
		}
		if(IS_POST){
			if(!isset($_SESSION['uid']) || !isset($_SESSION['uname'])){
				$this->error('请登陆');
			}
			$uid=$_SESSION['uid'];
//			金币
			$point=(int)$_POST['reward'];
			$time=time();
//			如果提交的话hd_user、hd_ask这两个表
//			修改user表加提问数，减金币，加经验
			$sql="UPDATE hd_user SET ask=ask+1,point=point-{$point},exp=exp+5 WHERE uid={$uid}";
			M()->exec($sql);
//			给ask插入一条数据(内容,金币,用户,分类,时间)
			$sql="INSERT INTO hd_ask SET content='{$_POST['content']}',reward={$point},uid={$uid},cid={$_POST['cid']},time={$time}";
			$asid=M()->exec($sql);
			$this->success("发布成功","./index.php?c=Ask&cid={$_POST['cid']}&asid={$asid}");
		}
		
		$this->display('ask.html');
	}
	/**
	 * 搜索操作
	 */
	public function search(){
		if(IS_POST){
			$content=$_POST['searcon'];
			$sql="SELECT * FROM hd_ask WHERE content like '%{$content}%' LIMIT 10";
			$searchData=M()->query($sql);
			if(empty($searchData)){
				$this->error("对不起没有查到您搜索的内容");
			}
			$data=M()->query("SELECT * FROM hd_category");
			$cate=$this->_fatherCate($data, $searchData[0]['cid']);
//			数组反转
			$cate=array_reverse($cate);
//			分配搜索内容分类变量
			$this->assign('cate',$cate);
//			分配搜索内容变量
			$this->assign('searchData',$searchData);
//			p($searchData);
			$this->display("search.html");			
			
			return;
		}
		$this->display("search.html");
	}



	
	/**
	 * 获得选择分类的子集
	 */
	public function getSon(){
		$cid=$_POST['cid'];
		$sql="SELECT * FROM hd_category WHERE pid={$cid}";
		$data=M()->query($sql);
		echo json_encode($data);
	}
	
	
 }
 
 
 
 
 
 
 
 
 
 
 


 ?>